package com.ning.farm_income_back.controller;

import com.github.pagehelper.PageInfo;
import com.ning.farm_income_back.domain.Costs;
import com.ning.farm_income_back.domain.Revenue;
import com.ning.farm_income_back.dto.CostsDto;
import com.ning.farm_income_back.dto.RevenueDto;
import com.ning.farm_income_back.service.RevenueService;
import com.ning.farm_income_back.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/revenue")
public class RevenueController {

    @Autowired
    private RevenueService revenueService;

    //分页带条件查询
    @PostMapping("/page")
    public ResultVo<PageInfo> page(@RequestBody RevenueDto revenueDto, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize ) {
        if(pageNum == null || pageNum < 1) pageNum = 1;

        //获取页面数据 总条数 总页数
        PageInfo pageInfo = revenueService.getPage(revenueDto,pageNum,pageSize);

        return ResultVo.success("页面数据查询成功",pageInfo);
    }

    //新增
    @PostMapping("/saveRevenue")
    public ResultVo saveRevenue(@RequestBody RevenueDto revenueDto,@RequestParam String username) {
        revenueService.saveRevenueInfo(revenueDto,username);
        return ResultVo.success("新增成功！");
    }

    //修改
    @PutMapping("/updateRevenue")
    public ResultVo updateRevenue(@RequestBody RevenueDto revenueDto,@RequestParam String username) {
        revenueService.updateRevenueInfo(revenueDto,username);
        return ResultVo.success("修改成功！");
    }

    //删除
    @DeleteMapping("/deleteRevenue/{id}")
    public ResultVo deleteRevenue(@PathVariable Integer id) {
        revenueService.deleteRevenueInfo(id);
        return ResultVo.success("删除成功！");
    }

    //获取年份区间的收益数组
    @GetMapping("/getRevenueList")
    public ResultVo<List<String>> getRevenueList(@RequestParam String startY, @RequestParam String endY){
        List<String> res = revenueService.getRevenueEveryYear(startY,endY);
        return ResultVo.success("获取成功",res);
    }

    //更新审核状态
    @PutMapping("/updateRevenueStatus")
    public ResultVo updateRevenueStatus(@RequestBody Revenue revenue) {
        revenueService.updateRevenueStatus(revenue);
        return ResultVo.success("更新成功！");
    }

    //审核驳回原因
    @PutMapping("/rejectRevenue")
    public ResultVo rejectRevenue(@RequestBody Revenue revenue) {
        revenueService.rejectRevenue(revenue);
        return ResultVo.success("更新成功！");
    }



}
